<template>
  <div class="yinyue">
    <!-- 背景墙 -->
    <div class="beijing">
      <img :src="yy.picUrl" />
    </div>
    <!-- logo图标 -->
    <div class="logo">
      <span>&#xe601;</span>网易云音乐
    </div>
    <!-- 音频 -->
    <div class="video_mus">
       <audio controls autoplay name="media"  @timeupdate="currentTimes"  ref="vd" class="video" :src="url.url">
       </audio>
    </div>
   
    <!-- 图片 -->
    <div class="tu">
      <img src="../assets/needle-ip6.png" alt="">
    </div>
    <!-- 中间圆盘 -->
    <div class="yuanpan">
          <div class="yuanpan_box">
            <div class="box_z" >
              <div :class="{'box_z2':active}" @click="zhuan">
                <div class="box_w">
                  <img src="../assets/disc.png" alt="">
                </div>
                <div class="box_n">
                  <img :src="yy.picUrl" alt="">
                </div>
              </div>
               <span v-if="over"  @click="zhuan" class="btn">&#xe623;</span>
            </div>
          </div>
            <!-- 歌词部分 -->
    <div class="gc">
      <p>{{yy.name}}-<span>{{name}}</span></p>
      <div :class="{'gc_p':true,'gc_p_v':curlyrs}">
        <div class="gc_pd" ref="cd">
           <p v-for="item in gec" :key="item.index">{{item.g}}</p>
        </div>
      </div>
    </div>
    </div>
  <!-- 查看歌词 -->
  <div class="chakan" @click="chakan">
    <p>查看完整歌词></p>
  </div>
  <!-- 更多 -->
  <div class="jt">
    <span>&#xe619;</span>
    <br>
    <span>&#xe619;</span>
  </div>
    <!-- 底部下载 -->
    <div class="dwon">
      <span>打开</span><span>下载</span>
    </div>
    <div class="kuai"></div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      id: this.$route.params.id,
      yy: {},
      active:  true,
      over: false,
      url: {},
      gec: [],
      name: '',
      curlyrs: true
    };
  },
  created() {
    this.getyy()
    this.getmusi()
    this.getgc()
  },
  methods: {
    // 获取音乐封面
    getyy() {
      // 注释没有作用
      this.$axios.get("/song/detail?ids=" + this.id).then(res => {
        console.log(res)
        this.yy = res.data.songs[0].al;
        this.name=res.data.songs[0].ar[0].name
      });
    },
    // 点击打开关闭音乐
    zhuan(){
       var audio = this.$refs.vd
      if(this.over==false){
           audio.pause() //点击停止播放，转盘不动
      }else{
          audio.play()  //默认自动播
      }
       this.over = !this.over
       this.active = !this.active
    },
    // 获取音乐url
    getmusi(){
      this.$axios.get("/song/url?id="+this.id)
       .then(res=>{
        //  console.log(res)
         this.url = res.data.data[0]
       })
    },
    // 获取歌词
    getgc(){
      this.$axios.get("/lyric?id="+this.id)
       .then(res=>{
        //  console.log(res)
        // this.gec = res.data.lrc.lyric
        // 换行符进行分割变成数组
        let lyrArr = res.data.lrc.lyric.split('\n')
        // 用来存放处理的数据
        let lyrObj = []
        // 遍历每一次昂，处理之后放进lyrObj
        for(let i of lyrArr){

          let t = i.substring(i.indexOf("[")+1,i.indexOf("]"));
        
          let time = (t.split(":")[0]*60+parseFloat(t.split(":")[1])).toFixed(3);

          if(time){
            lyrObj.push({
              't':time,
              'g':i.substring(i.indexOf("]")+1,i.length),
            })
          }
        }
        this.gec = lyrObj
       })
    },
    // 获取全部歌词
    chakan(){
      this.curlyrs = !this.curlyrs
    },
    // 音频事件
    currentTimes(){
                // 不暂停直接退出去的话会有报错，所以要return
                if(!this.$refs.vd) return
                // 点击播放监听当前播放时间
                let t = this.$refs.vd.currentTime + 0.5;
                // console.log(this.$refs.audio.currentTime)
                // 循环存好的数据，拿数据里面的时间和歌曲播放的当前时间作对比，如果当前时间大于等于数据里面的时间，表示为正在播放的歌词
                // 然后把和时间对应的歌词取出来渲染
                for(let i in this.gec){
                    if(t >= this.gec[i]['t']){
                        
                       this.$refs.cd.style.transform = `translateY(-${i*25}px)`
                      
                        // 歌词去重，并且不为空渲染到页面
                        // if(this.curlyr != this.gec[i]['g'] && this.gec[i]['g']){
                            
                        //}
                       
                    }
                   
             
                }
              
                
            }
  }
};
</script>

<style scoped>
.video{
  width:100px;
  height:100px;
  z-index: 200;
  opacity: 0;
  position: absolute;
}
.yinyue{
  width:100%;
  position:relative;
}
.beijing {
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.8);
  overflow: hidden;
  position: fixed;
  z-index: -99;
}
.beijing > img {
  width: 100%;
  height: 100%;
  filter: blur(30px);
}
.logo{
  position:absolute;
  left:10px;
  top:10px;
  color:white;
  font-size:15px;
}
.logo>span{
  font-family: "wy";
  font-size:15px;
  color:#D43C33;
}
.yuanpan{
  height:540px;
}
.yuanpan_box{
  padding-top:50px;
}
.box_z{
  position: relative;
    width:296px;
  height:296px;
  margin:0 auto;
  overflow: hidden;
}
.box_z>span{
  font-family: "wy";
  font-size:50px;
  color:white;
  position: absolute;
  left:50%;
  top:50%;
  width:50px;
  height:50px;
  margin:-25px 0 0 -25px;
}
.box_z2{
  width:296px;
  height:296px;
  position: absolute;
  animation: zz 15s linear infinite;
}

@keyframes zz {
  0%{
    transform: rotate(0);
  }
  100%{
    transform: rotate(360deg);
  }
}
.box_w{
  width:100%;
  height:100%;
}
.box_w>img{
  width:100%;
  height:100%;
}
.box_n{
  position: absolute;
  width:184px;
  height:184px;
  left:50%;
  top:50%;
  margin:-92px 0 0 -92px;
}
.box_n>img{
  width:100%;
  height:100%;
  border-radius: 50%;
}
.dwon{
  width:100%;
  padding:8px 15px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: fixed;
  bottom: 0;
}
.dwon>span:nth-child(1){
  width:45%;
  height:40px;
  line-height: 40px;
  text-align: center;
  border:1px solid #D43C33;
  color:#D43C33;
  letter-spacing: 5px;
  display: inline-block;
  border-radius: 40px;
}
.dwon>span:nth-child(2){
    width:45%;
  height:40px;
  line-height: 40px;
  text-align: center;
  border:1px solid #D43C33;
  color:white;
  letter-spacing: 5px;
  display: inline-block;
  background-color:#D43C33;
  border-radius: 40px;
}
.tu{
  position: absolute;
  width:100px;
  left:50%;
  z-index: 3;
}
.tu>img{
  width:100%;
}

.gc{
  width: 100%;
  padding:20px 40px;
  text-align: center;
  color: white;
  font-size: 16px;
}
.gc>p{
  margin:5px 0 20px;
}
.gc>p>span{
  font-size:14px;
  color:hsla(0,0%,100%,.6);
}
.gc_p>p{
  margin:5px 0;
}
.chakan{
  width:100%;
  text-align: center;
  position: relative;
  z-index: 99;
}
.chakan>p{
  color:white;
  font-size: 14px;
  color:hsla(0,0%,100%,.6);
}
.jt{
  width:100%;
 text-align: center;
 padding:6px 0;
}
.jt>span:nth-of-type(1){
  font-family: "wy";
  color:hsla(0,0%,100%,.6);
  animation: over 1s linear infinite ;
  margin-bottom: -30px;
  display: block;
}
.jt>span:nth-of-type(2){
  font-family: "wy";
  color:hsla(0,0%,100%,.6);
  animation: over2 1s linear infinite ;
  animation-delay: 0.3s;
}
@keyframes over{
    0%{
      opacity: 1;
    }
    100%{
      opacity: 0.2;
    }
}
@keyframes over2{
    0%{
      opacity: 1;
    }
    100%{
      opacity: 0.2;
    }
}
.gc_p{
  width:100%;
  -webkit-mask: -webkit-linear-gradient(top,#000,#000 70%,rgba(0,0,0,0));
}
.gc_p_v{
    overflow: hidden;
    height:50px;
}
.gc_pd{
  transition: all 0.3s linear;
}
.gc_pd>p{
  height:25px;
  line-height: 25px;
  font-size:16px;
  /* color:hsla(0,0%,100%,.6); */
}
/* .gc_pd>p:nth-of-type(1){
  color:white;
} */
</style>